Retrieving the X and Y coordinates of a form field is useful if you wish to use a form field as a placeholder on the PDF document. For instance, you may want to place a Form Field on a PDF template so that the location of that form field could later be used to place an image, barcode, etc. on the final output document.
The following example demonstrates how to read the X and Y location of a form field and then places an image at that same location. This example also pulls the Height and Width of the form field so that those values can be used when placing the image on the PDF:
[Java] PdfDocument pdfDocument = new PdfDocument("[PhysicalPath]/template.pdf");
MergeDocument document = new MergeDocument(pdfDocument, MergeOptions.getNone());
Page page = document.getPages().getPage(0);
Image image = null;
try {
image = new Image("[PhysicalPath]/image.gif", 0, 0);
} catch (Exception ex) {
System.out.println("Input Exception");
}
image.setX(pdfDocument.getForm().getFields().getPdfFormField("PlaceHolderField").getX(page));
image.setY(pdfDocument.getForm().getFields().getPdfFormField("PlaceHolderField").getY(page));
image.setHeight(pdfDocument.getForm().getFields().getPdfFormField("PlaceHolderField").getHeight());
image.setWidth(pdfDocument.getForm().getFields().getPdfFormField("PlaceHolderField").getWidth());
page.getElements().add(image);
document.draw("[PhysicalPath] ");